<template>
  <div class="show-main">
    <el-scrollbar style="height:100%">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline" size="small">
        <el-form-item>
          <el-button type="success" @click.stop="search">刷新</el-button>
          <el-button type="warning" @click.stop="add">添加</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="table" border size="small" style="width: 500px;">
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="name" label="范围名称"></el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row.id)">编辑</el-button>
            <el-button type="text" size="small" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="searchForm.page"
          :page-sizes="[10,20,30,40]" :page-size="searchForm.limit" layout="total, sizes, prev, pager, next, jumper" :total="count">
        </el-pagination>
      </div>
      <el-dialog title="添加经营范围" :visible.sync="addVisible" width="500px" :before-close="handleClose('addForm')">
        <el-form ref="addForm" :model="addForm" label-width="120px" size="small" :rules="rules">
          <el-form-item label="经营范围名称" prop="name">
            <el-input v-model="addForm.name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="close('addVisible', 'addForm')" size="small">取 消</el-button>
          <el-button type="primary" @click="send('addVisible','addForm')" size="small">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="编辑经营范围" :visible.sync="editVisible" width="500px" :before-close="handleClose('editForm')">
        <el-form ref="editForm" :model="editForm" label-width="120px" size="small" :rules="rules">
          <el-form-item label="经营范围名称" prop="name">
            <el-input v-model="editForm.name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="close('editVisible', 'editForm')" size="small">取 消</el-button>
          <el-button type="primary" @click="send('editVisible','editForm')" size="small">确 定</el-button>
        </span>
      </el-dialog>
    </el-scrollbar>
  </div>
</template>
<script>
import { scopeList, scopeDelete, scopeEdit, scopeInc, scopeSave } from '@/network/api.js'

export default {
  name: 'scope',
  data() {
    return {
      searchForm: {
        page: 1,
        limit: 10
      },
      count: 0, // 总记录数
      table: [],
      addForm: {
        name: '' // 经营范围名称
      }, // 添加的表单
      editForm: {
        id: 0,
        name: '' // 经营范围名称
      }, // 编辑的表单
      addVisible: false, // 添加表单
      editVisible: false, // 编辑表单
      rules: {
        name: [{ required: true, message: '请输入经营范围名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.search()
  },
  computed: {},
  methods: {
    async search(pageInit = false) {
      // 获取经营范围列表
      try {
        if (pageInit === true) {
          this.searchForm.page = 1
        }
        const rtn = await scopeList(this.searchForm)
        this.table = rtn.data.list
        this.count = rtn.data.count
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 每页条数变化
    handleSizeChange(val) {
      this.searchForm.limit = val
      this.search()
    },
    // 当前页变化
    handleCurrentChange(val) {
      this.searchForm.page = val
      this.search()
    },
    // 添加经营范围
    add() {
      this.addVisible = true
    },
    // 编辑经营范围
    async edit(id) {
      try {
        const rtn = await scopeEdit({ id })
        this.editVisible = true
        this.fileList = []
        this.$nextTick(() => {
          this.editForm = rtn.data
        })
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 删除
    async del(id) {
      try {
        await scopeDelete({ id })
        this.search()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 关闭弹出前操作(不包括取消按钮)
    handleClose(formName) {
      return done => {
        this.$refs[formName].resetFields()
        done()
      }
    },
    // 取消按钮
    close(visibleName, formName) {
      this.$refs[formName].resetFields()
      this[visibleName] = false
    },
    send(visibleName, formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          switch (formName) {
            case 'addForm': // 添加表单
              try {
                await scopeInc(this.addForm)
                this.close('addVisible', 'addForm')
                this.search()
              } catch (e) {
                this.$message.error(e.message)
              }
              break
            case 'editForm': // 编辑表单
              try {
                await scopeSave(this.editForm)
                this.close('editVisible', 'editForm')
                this.search()
              } catch (e) {
                this.$message.error(e.message)
              }
              break
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
</style>
